$("#add_project").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#interface").css("display", "none");
	$("#display_interface").css("display", "none");
	$("#add_authorization_detail").css("display", "none");
	$("#authentication_list").css("display", "none");
	$("#add_server_monitor").css("display", "none");
	$("#add_link_test").css("display", "none");
	$("#add_global_parameter").css("display", "none");
	$("#display_link_list").css("display", "none");
    $("#project").fadeIn(135);
});
$("#add_module").click(function () {
	$("#project").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#interface").css("display", "none");
	$("#display_interface").css("display", "none");
	$("#add_authorization_detail").css("display", "none");
	$("#authentication_list").css("display", "none");
	$("#add_server_monitor").css("display", "none");
	$("#add_link_test").css("display", "none");
	$("#add_global_parameter").css("display", "none");
	$("#display_link_list").css("display", "none");
    $("#module").fadeIn(135);
});
$("#dashboard").click(function () {
	$("#project").css("display", "none");
	$("#face_list").css("display", "none");
	$("#interface").css("display", "none");
    $("#module").css("display", "none");
    $("#display_interface").css("display", "none");
    $("#add_authorization_detail").css("display", "none");
    $("#authentication_list").css("display", "none");
    $("#add_server_monitor").css("display", "none");
    $("#add_link_test").css("display", "none");
	$("#add_global_parameter").css("display", "none");
	$("#display_link_list").css("display", "none");
    $("#right-content").fadeIn(135);
});
$("#add_interface").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
	$("#add_authorization_detail").css("display", "none");
	$("#authentication_list").css("display", "none");
	$("#add_server_monitor").css("display", "none");
	$("#add_link_test").css("display", "none");
	$("#add_global_parameter").css("display", "none");
	$("#display_link_list").css("display", "none");
    $("#interface").fadeIn(135);
});
$("#add_authorization").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
    $("#interface").css("display", "none");
    $("#authentication_list").css("display", "none");
    $("#add_server_monitor").css("display", "none");
    $("#add_link_test").css("display", "none");
    $("#add_global_parameter").css("display", "none");
    $("#display_link_list").css("display", "none");
    $("#add_authorization_detail").fadeIn(135);
});
$("#server_monitor").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
    $("#interface").css("display", "none");
    $("#authentication_list").css("display", "none");
    $("#add_authorization_detail").css("display", "none");
    $("#add_link_test").css("display", "none");
    $("#add_global_parameter").css("display", "none");
    $("#display_link_list").css("display", "none");
    $("#add_server_monitor").fadeIn(135);
});

$("#global_parameter").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
    $("#interface").css("display", "none");
    $("#authentication_list").css("display", "none");
    $("#add_authorization_detail").css("display", "none");
    $("#add_server_monitor").css("display", "none");
    $("#add_link_test").css("display", "none");
    $("#display_link_list").css("display", "none");
    $("#add_global_parameter").fadeIn(135);
    
    // 查询全局参数并在页面显示
    $.ajax({
		cache : true,
		type : "POST",
		url : "/get_global_parameter",
		async : true,
		success : function(data) {
			if(data!=""){
				
				$("#tb-global-parameter").empty();
				
				var tb_th = "<tr><th class='th' style='width:45%'>参数名</th><th style='width:45%' class='th'>参数值</th><th style='width:10%' class='th'>操作</th></tr>";
				$("#tb-global-parameter").append(tb_th);
				for(var key in data){
					var tab = document.getElementById("tb-global-parameter");
					var rows = tab.rows.length;
					var tb_tr = "<tr class='td'><td class='td'><input type='text' value='"+key+"' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数名'/></td><td class='td'><input type='text' value='"+data[key]+"' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数值'/></td><td style='text-align: center;'><a href='javascript:void(0)' onclick='delete_parameter(this)'>删除</td></tr>";
					$("#tb-global-parameter").append(tb_tr);
				}
			}else{
				return false;
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
    
});
$("#link_test").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
    $("#interface").css("display", "none");
    $("#authentication_list").css("display", "none");
    $("#add_authorization_detail").css("display", "none");
    $("#add_server_monitor").css("display", "none");
    $("#add_global_parameter").css("display", "none");
    $("#display_link_list").css("display", "none");
    $("#add_link_test").fadeIn(135);
    
    // 清空由于查询链路遗留下的数据
    $("#link_picture").empty();
	$("#link_name").val("");
	$("#link_detail").text("");
	$("#hidden_link_id").empty();
});

$("#link_list").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
	$("#interface").css("display", "none");
	$("#authentication_list").css("display", "none");
	$("#add_authorization_detail").css("display", "none");
	$("#add_link_test").css("display", "none");
	$("#add_global_parameter").css("display", "none");
	$("#add_server_monitor").css("display", "none");
	$("#display_link_list").fadeIn(135);
	
	$.ajax({
		cache : false,
		type : "GET",
		url : "/get_link_list",
		async : true,
		success : function(data) {
			
			$("#tb_link_list").empty();
			var th = "<tr><th style='width:10%'>链路ID</th><th  style='width:20%'>链路名称</th><th  style='width:30%'>链路节点序列</th><th  style='width:30%'>链路详情描述</th><th style='width:10%'>操作</th></tr>";
		    $("#tb_link_list").append(th);
		    //alert(data[0].linkId)
		    if(data.length==0){
		    	var ps = "<tr><td colspan='8' style='text-align:center;font-weight:bold;color:red;'>没有查询任何链路，快去添加新的链路吧。</td></tr>";
		    	$("#tb_link_list").append(ps);
		    }else{
		    	for (var i=0;i<data.length;i++){
		    		var content = "<tr><td>"+data[i].linkId+"</td><td><a href='javascript:void(0)' onclick='find_link_by_id("+data[i].linkId+")'>"+data[i].linkName+"</a></td><td>"+data[i].linkNodeSequence+"</td><td>"+data[i].linkDetail+"</td><td style='text-align:center;text-indent:-5px;'><a href='javascript:void(0)' onclick='link_execute_by_id("+data[i].linkId+")'>执行</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='delete_link_by_id("+data[i].linkId+")'>删除</a></td></tr>";
		    		$("#tb_link_list").append(content);
		    	}
		    }
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
	
	$.ajax({
		cache : false,
		type : "GET",
		url : "/get_link_list_count",
		async : true,
		success : function(data) {
			$("#total_link").val(data);
			$("#page_total_link").text("R"+data);
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
	
	
	
});

$("#authorization_center").click(function () {
	$("#module").css("display", "none");
	$("#right-content").css("display", "none");
	$("#face_list").css("display", "none");
	$("#project").css("display", "none");
	$("#display_interface").css("display", "none");
    $("#interface").css("display", "none");
    $("#add_authorization_detail").css("display", "none");
    $("#add_server_monitor").css("display", "none");
    $("#add_link_test").css("display", "none");
	$("#add_global_parameter").css("display", "none");
	$("#display_link_list").css("display", "none");
    $("#authentication_list").fadeIn(135);

	$.ajax({
		cache : false,
		type : "GET",
		url : "/find_authorization",
		async : true,
		success : function(data) {
			
			$("#tb_authentication_list").empty();
			var th = "<tr><th style='width:10%'>认证ID</th><th  style='width:10%'>权限名称</th><th  style='width:20%'>认证类型</th><th  style='width:10%'>用户名</th><th  style='width:10%'>密码</th><th  style='width:20%'>token</th><th  style='width:10%'>remark</th><th style='width:10%'>操作</th></tr>";
		    $("#tb_authentication_list").append(th);
		    if(data.length==0){
		    	var tips = "<tr><td colspan='8' style='text-align:center;font-weight:bold;color:red;'>没有查询任何权限，快去添加权限吧。</td></tr>";
		    	$("#tb_authentication_list").append(tips);
		    }else{
		    	for (var i=0;i<data.length;i++){
		    		var tb = "<tr><td>"+data[i].authenticationId+"</td><td><a href='javascript:void(0)' onclick='find_authentication_by_id("+data[i].authenticationId+")'>"+data[i].authorizationName+"</a></td><td>"+data[i].authenticationType+"</td><td>"+data[i].username+"</td><td>"+data[i].password+"</td><td>"+data[i].token+"</td><td>"+data[i].remark+"</td><td style='text-align:center;'><a href='javascript:void(0)' onclick='delete_authorization_by_id("+data[i].authenticationId+")'>删除</a> <a href='javascript:void(0)' onclick='disable_auth("+0000+data[i].authenticationId+")'>启用</a> <a href='javascript:void(0)' onclick='disable_auth("+0000+data[i].authenticationId+")'>禁用</a></td></tr>";
		    		$("#tb_authentication_list").append(tb);
		    	}
		    }
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
    
});

function add_module() {
	
	// 校验
	var module_name = $("#module_name").val();
	if(module_name==""){
		if($('#module_tips').length == 0){
			$("#module_name").after("<span id='module_tips' style='color:red;'>模块名称不能为空！<span>");
			return false;
		}else if($('#module_tips').length > 1){
			$("#module_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#module_tips").remove();
	}
	
	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_module",
		data : $('#add-module-form').serialize(),
		async : false,
		success : function(data) {
			if (data == 1) {
				alert("模块创建成功啦，快去给新模块添加接口吧！");
				window.location.reload();
			} else {
				alert("failed");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function add_project() {
	
	// 校验
	var project_name = $("#project_name").val();
	if(project_name==""){
		if($("#project_tips").length == 0){
			$("#project_name").after("<span id='project_tips' style='color:red;'>项目名称不能为空！<span>");
			return false;
		}else if($("#project_tips").length > 1){
			$("#project_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#project_tips").remove();
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_project",
		data : $('#add-project-form').serialize(),
		async : false,
		success : function(data) {
			if (data == 1) {
				alert("项目创建成功啦，快去给新项目添加模块吧！");
				window.location.reload();
			} else {
				alert("failed");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

$(".menu").click(function(ev){
	var ev = ev||window.event;
	var target = ev.target || ev.srcElement;
	if(target.nodeName.toLowerCase() == "span"){
		var moduleName = target.innerHTML;
		var moduleId = target.getAttribute('name');

		$.ajax({
			cache : false,
			type : "POST",
			url : "/get_face_list_by_module_id?moduleId="+moduleId,
			async : true,
			success : function(data) {
				$("#module").css("display", "none");
				$("#right-content").css("display", "none");
			    $("#project").css("display", "none");
			    $("#interface").css("display", "none");
			    $("#display_interface").css("display", "none");
			    $("#add_authorization_detail").css("display", "none");
			    $("#authentication_list").css("display", "none");
			    $("#add_server_monitor").css("display", "none");
			    $("#add_link_test").css("display", "none");
				$("#add_global_parameter").css("display", "none");
				$("#display_link_list").css("display", "none");
			    $("#face_list").fadeIn(135);
			    
			    $("#face_list_module_name").text(moduleName+" - 接口列表");
			    $("#tb_face_list").empty();
			    var th = "<tr><th style='width:5%'>接口ID</th><th style='width:12%'>接口名称</th><th style='width:28%'>接口地址</th><th style='width:5%'>接口协议</th><th style='width:5%'>请求方法</th><th style='width:30%'>body</th><th style='width:5%'>认证ID</th><th style='width:10%'>接口操作</th></tr>";
			    $("#tb_face_list").append(th);
			    if(data.length==0){
			    	var tips = "<tr><td colspan='9' style='text-align:center;font-weight:bold;color:red;'>没有查询到该模块下的接口，快去添加接口吧。</td></tr>";
			    	$("#tb_face_list").append(tips);
			    }else{
			    	for (var i=0;i<data.length;i++){
			    		var tb = "<tr><td>"+data[i].faceId+"</td><td><a href='javascript:void(0)' onclick='find_face_by_id("+data[i].faceId+")'>"+data[i].faceName+"</a></td><td>"+data[i].faceUrl+"</td><td>"+data[i].faceProtocol+"</td><td>"+data[i].faceMethod+"</td><td>"+data[i].faceBody+"</td><td>"+data[i].authenticationId+"</td><td style='text-align:center;text-indent:-5px;'><a href='javascript:void(0)' onclick='execute_face("+data[i].faceId+")'>执行</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='delete_face_by_face_id("+data[i].faceId+");reload_delete_face_by_face_id("+data[i].moduleId+")'>删除</a></td></tr>";
			    		$("#tb_face_list").append(tb);
			    	}
			    	// tip:记录moduleId，分页查询使用
			    	$("#page_module_id").attr("value",moduleId);
			    	$("#currentPage").text(1);
			    }
			},
			error : function(request) {
				alert("Connection error:" + request.error);
			}
		});
	}

	$.ajax({
		cache : false,
		type : "GET",
		url : "/get_face_list_by_module_id_count?moduleId="+moduleId,
		async : true,
		success : function(data) {
			$("#total_record").val(data);
			$("#page_total_record").text("R"+data);
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
})

function add_interface() {
	
	// 添加接口模块校验
	var vface_name = $("#face_name").val();
	if(vface_name==""){
		if($('#vface_name_tips').length == 0){
			$("#face_name").after("<span id='vface_name_tips' style='color:red;'>接口名称不能为空！<span>");
			return false;
		}else if($('#vface_name_tips').length > 1){
			$("#vface_name_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vface_name_tips").remove();
	}
	
	var vface_url = $("#face_url").val();
	if(vface_url==""){
		if($("#vface_url_tips").length == 0){
			$("#face_url").after("<span id='vface_url_tips' style='color:red;'>接口地址不能为空！<span>");
			return false;
		}else if($("#vface_url_tips").length > 1){
			$("#vface_url_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vface_url_tips").remove();
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_interface",
		data : $('#add-interface-form').serialize(),
		async : false,
		success : function(data) {
			var strs = data.split("@");
			if (strs[0] == "1") {
				alert("添加接口成功！");
				$("[name='"+strs[1]+"']").click();
			} else {
				alert("failed");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function execute_face(faceId) {
	$.ajax({
		cache : true,
		type : "GET",
		url : "/execute_face?faceId="+faceId,
		async : true,
		success : function(data) {
			//alert(data);
			showbody(data);
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function find_face_by_id(faceId) {
	$.ajax({
		cache : false,
		type : "GET",
		url : "/find_face_by_id?faceId="+faceId,
		async : true,
		success : function(data) {
			$("#module").css("display", "none");
			$("#right-content").css("display", "none");
			$("#face_list").css("display", "none");
			$("#project").css("display", "none");
		    $("#interface").css("display", "none");
		    $("#add_server_monitor").css("display", "none");
		    $("#add_link_test").css("display", "none");
			$("#add_global_parameter").css("display", "none");
			$("#display_link_list").css("display", "none");
		    $("#display_interface").fadeIn(135);
		    // alert(data.faceId);
		   
		    // 追加之前先清空
		    $("#display_module_id").empty();
		    $("#display_face_method").empty();
		    $("#display_face_protocol").empty();
		    $("#display_face_host").empty();
		    $("#display_auth_name").empty();
		    $("display_face_name").val("");
		    $("display_face_url").val("");
		    $("display_face_id").val("");
		    
		    // 为Select追加一个Option(下拉项)
		    for (var i=0;i<data.length;i++){
		    	for (var x=0;x<data[1].length;x++){
			    	if(data[1][x].moduleId==data[0].moduleId){
			    		$("#display_module_id").append("<option value=\""+data[0].moduleId+"\">"+data[1][x].moduleName+"</option>");
			    		break;
			    	}
		    	}
		    	break;
    		}
		    
		    for (var i=0;i<data.length;i++){
		    	
		    	$("#display_face_name").attr("value",data[0].faceName);
		    	$("#display_face_id").attr("value",data[0].faceId);
		    	if("GET"==data[0].faceMethod){
		    		$("#display_face_method").append("<option selected=\"selected\" value=\""+data[0].faceMethod+"\">"+data[0].faceMethod+"</option>");
		    		$("#display_face_method").append("<option value=\"POST\">POST</option>");
		    		$("#display_face_method").append("<option value=\"PUT\">PUT</option>");
		    		$("#display_face_method").append("<option value=\"DELETE\">DELETE</option>");
		    	}
		    	if("POST"==data[0].faceMethod){
		    		$("#display_face_method").append("<option value=\"GET\">GET</option>");
		    		$("#display_face_method").append("<option selected=\"selected\" value=\""+data[0].faceMethod+"\">"+data[0].faceMethod+"</option>");
		    		$("#display_face_method").append("<option value=\"PUT\">PUT</option>");
		    		$("#display_face_method").append("<option value=\"DELETE\">DELETE</option>");
		    	}
		    	if("PUT"==data[0].faceMethod){
		    		$("#display_face_method").append("<option value=\"GET\">GET</option>");
		    		$("#display_face_method").append("<option value=\"POST\">POST</option>");
		    		$("#display_face_method").append("<option selected=\"selected\" value=\""+data[0].faceMethod+"\">"+data[0].faceMethod+"</option>");
		    		$("#display_face_method").append("<option value=\"DELETE\">DELETE</option>");
		    	}
		    	if("DELETE"==data[0].faceMethod){
		    		$("#display_face_method").append("<option value=\"GET\">GET</option>");
		    		$("#display_face_method").append("<option value=\"POST\">POST</option>");
		    		$("#display_face_method").append("<option value=\"PUT\">PUT</option>");
		    		$("#display_face_method").append("<option selected=\"selected\" value=\""+data[0].faceMethod+"\">"+data[0].faceMethod+"</option>");
		    	}
		    	
		    	if("HTTP"==data[0].faceProtocol){
		    		$("#display_face_protocol").append("<option selected=\"selected\" value=\""+data[0].faceProtocol+"\">"+data[0].faceProtocol+"</option>");
		    		$("#display_face_protocol").append("<option value=\"HTTPS\">HTTPS</option>");
		    		$("#display_face_protocol").append("<option value=\"Dubbo\">Dubbo</option>");
		    		$("#display_face_protocol").append("<option value=\"WebService\">WebService</option>");
		    	}
		    	if("HTTPS"==data[0].faceProtocol){
		    		$("#display_face_protocol").append("<option value=\"HTTP\">HTTP</option>");
		    		$("#display_face_protocol").append("<option selected=\"selected\" value=\""+data[0].faceProtocol+"\">"+data[0].faceProtocol+"</option>");
		    		$("#display_face_protocol").append("<option value=\"Dubbo\">Dubbo</option>");
		    		$("#display_face_protocol").append("<option value=\"WebService\">WebService</option>");
		    	}
		    	if("Dubbo"==data[0].faceProtocol){
		    		$("#display_face_protocol").append("<option value=\"HTTP\">HTTP</option>");
		    		$("#display_face_protocol").append("<option value=\"HTTPS\">HTTPS</option>");
		    		$("#display_face_protocol").append("<option selected=\"selected\" value=\""+data[0].faceProtocol+"\">"+data[0].faceProtocol+"</option>");
		    		$("#display_face_protocol").append("<option value=\"WebService\">WebService</option>");
		    	}
		    	if("WebService"==data[0].faceProtocol){
		    		$("#display_face_protocol").append("<option value=\"HTTP\">HTTP</option>");
		    		$("#display_face_protocol").append("<option value=\"HTTPS\">HTTPS</option>");
		    		$("#display_face_protocol").append("<option value=\"Dubbo\">Dubbo</option>");
		    		$("#display_face_protocol").append("<option selected=\"selected\" value=\""+data[0].faceProtocol+"\">"+data[0].faceProtocol+"</option>");
		    	}
		    	$("#display_face_body").val(data[0].faceBody);
			    $("#display_face_url").attr("value",data[0].faceUrl);
			    for(var y=0;y<data[2].length;y++){
			    	if(data[0].authenticationId==data[2][y].authenticationId){
			    		$("#display_auth_name").append("<option selected=\"selected\" value=\""+data[0].authenticationId+"\">"+data[2][y].authorizationName+"</option>");
			    	}else{
			    		$("#display_auth_name").append("<option value=\""+data[2][y].authenticationId+"\">"+data[2][y].authorizationName+"</option>");
			    	}
		    	}
			    for(var key in data[3]){
			    	if(key==data[0].faceHost){
			    		$("#display_face_host").append("<option selected=\"selected\" value=\""+key+"\">"+key+"</option>");
			    	}else{
			    		$("#display_face_host").append("<option value=\""+key+"\">"+key+"</option>");
			    	}
			    }
			    break;
		    }
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function update_interface() {
	
	// 校验必填项
	var vdisplay_face_name = $("#display_face_name").val();
	if(vdisplay_face_name==""){
		if($('#vdisplay_face_name_tips').length == 0){
			$("#display_face_name").after("<span id='vdisplay_face_name_tips' style='color:red;'>接口名称不能为空！<span>");
			return false;
		}else if($('#vdisplay_face_name_tips').length > 1){
			$("#vdisplay_face_name_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vdisplay_face_name_tips").remove();
	}

	var vdisplay_face_url = $("#display_face_url").val();
	if(vdisplay_face_url==""){
		if($("#vdisplay_face_url_tips").length == 0){
			$("#display_face_url").after("<span id='vdisplay_face_url_tips' style='color:red;'>接口地址不能为空！<span>");
			return false;
		}else if($("#vdisplay_face_url_tips").length > 1){
			$("#vdisplay_face_url_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vdisplay_face_url_tips").remove();
	}
	
	$.ajax({
		cache : true,
		type : "POST",
		url : "/update_face",
		data : $('#update-interface-form').serialize(),
		async : true,
		success : function(data) {
			if (data == 1) {
				alert("更新成功！");
			} else {
				alert("更新失败，请确认后重试！");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function add_authorization() {
	
	// 授权模块提交时校验
	var vauthorization_name = $("#authorization_name").val();
	if(vauthorization_name==""){
		if($('#authorization_name_tips').length == 0){
			$("#authorization_name").after("<span id='authorization_name_tips' style='color:red;'>模块名称不能为空！<span>");
			return false;
		}else if($('#authorization_name_tips').length > 1){
			$("#authorization_name_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#authorization_name_tips").remove();
	}

	
	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_authorization",
		data : $('#add-authorization-form').serialize(),
		async : true,
		success : function(data) {
			$("#authentication_id").empty();
			for(var i=0;i<data.length;i++){
				var opt = "<option value='"+data[i].authenticationId+"'>"+data[i].authorizationName+"</option>";
				$("#authentication_id").append(opt);
			}
			alert("添加授权认证成功！");
			$("#authorization_center").click();
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function delete_authorization_by_id(authenticationId){
	if (confirm("确定要删除认证 ID 为 "+authenticationId+" 的数据吗？\r\n删除以后本条数据将不可恢复！！！")==true){
		$.ajax({
			cache : false,
			type : "GET",
			url : "/delete_authorization_by_id?authenticationId="+authenticationId,
			async : true,
			success : function(data) {
				if (data == 1) {
					$("#authorization_center").click();
				} else {
					alert("未成功删除数据，请确认后再次操作！");
				}
			},
			error : function(request) {
				alert("Connection error:" + request.error);
			}
		});
	}else{
		return false;
	}
}

function delete_face_by_face_id(faceId){
	if (confirm("确定要删除接口 ID 为 "+faceId+" 的数据吗？\r\n删除以后本条数据将不可恢复！！！")==true){
		$.ajax({
			cache : false,
			type : "GET",
			url : "/delete_face_by_face_id?faceId="+faceId,
			async : true,
			success : function(data) {
			},
			error : function(request) {
				alert("Connection error:" + request.error);
			}
		});
	}else{
		return false;
	}
}

function reload_delete_face_by_face_id(moduleId){
	$("[name='"+moduleId+"']").click();
}

/*$("#f5").click(function f5(){
	window.location.reload();
})*/

function page(id) {
	var pageSize=25;
	var currentPage=1;
	var fromPage=1;
	var toPage=1;
	
	// 首页
	if(id==1){
		currentPage = $("#currentPage").text();
		if(currentPage>1){
			currentPage=1;
			fromPage=0;
			toPage=pageSize;
		}else{
			return false;
		}
	}
	
	// 上一页
	if(id==2){
		currentPage = $("#currentPage").text();
		if(currentPage>1){
			fromPage = currentPage*pageSize-pageSize-pageSize;
			toPage = pageSize;
			currentPage--;
		}else{
			return false;
		}
	}
	
	// 下一页
	if(id==4){
		var total_record = $("#total_record").val();
		currentPage = $("#currentPage").text();
		if(currentPage*pageSize<total_record){
			
			fromPage = (currentPage*pageSize);
			toPage = pageSize;
			currentPage++;
		}else{
			return false;
		}
	}
	
	// 尾页
	if(id==5){
		var total_record = $("#total_record").val();
		if(total_record>25 || total_record==25){
			if(total_record%pageSize==0){
				var pageCount = total_record/pageSize;
				fromPage = (pageCount-1)*pageSize;
				toPage = pageSize;
				currentPage = total_record/pageSize;
			}else{
				yushu = total_record%pageSize;
				pageCount = parseInt(total_record/pageSize);
				fromPage = pageCount*pageSize;
				toPage = yushu;
				currentPage = parseInt(total_record/pageSize)+1;
			}
		}else{
			return false;
		}
	}
	
	var moduleId = $("#page_module_id").val();
	
	$.ajax({
		cache : false,
		type : "GET",
		url : "/get_face_list_by_module_id_page?moduleId="+moduleId+"&pageSize=30&fromPage="+fromPage+"&toPage="+toPage,
		async : true,
		success : function(data) {
			$("#module").css("display", "none");
			$("#right-content").css("display", "none");
		    $("#project").css("display", "none");
		    $("#interface").css("display", "none");
		    $("#display_interface").css("display", "none");
		    $("#add_authorization_detail").css("display", "none");
		    $("#authentication_list").css("display", "none");
		    $("#add_server_monitor").css("display", "none");
		    $("#add_link_test").css("display", "none");
			$("#add_global_parameter").css("display", "none");
			$("#display_link_list").css("display", "none");
		    $("#face_list").fadeIn(135);
		    
		    $("#tb_face_list").empty();
		    var th = "<tr><th style='width:5%'>接口ID</th><th style='width:12%'>接口名称</th><th style='width:28%'>接口地址</th><th style='width:5%'>接口协议</th><th style='width:5%'>请求方法</th><th style='width:30%'>body</th><th style='width:5%'>认证ID</th><th style='width:10%'>接口操作</th></tr>";
		    $("#tb_face_list").append(th);
		    if(data.length==0){
		    	var tips = "<tr><td colspan='9' style='text-align:center;font-weight:bold;color:red;'>没有查询到该模块下的接口，快去添加接口吧。</td></tr>";
		    	$("#tb_face_list").append(tips);
		    }else{
		    	for (var i=0;i<data.length;i++){
		    		var tb = "<tr><td>"+data[i].faceId+"</td><td><a href='javascript:void(0)' onclick='find_face_by_id("+data[i].faceId+")'>"+data[i].faceName+"</a></td><td>"+data[i].faceUrl+"</td><td>"+data[i].faceProtocol+"</td><td>"+data[i].faceMethod+"</td><td>"+data[i].faceBody+"</td><td>"+data[i].authenticationId+"</td><td style='text-align:center;text-indent:-5px;'><a href='javascript:void(0)' onclick='execute_face("+data[i].faceId+")'>执行</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='delete_face_by_face_id("+data[i].faceId+");reload_delete_face_by_face_id("+data[i].moduleId+")'>删除</a></td></tr>";
		    		$("#tb_face_list").append(tb);
		    	}
		    	$("#currentPage").text(currentPage);
		    }
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function findLinkListPage(id) {
	var pageSize=25;
	var currentPage=1;
	var fromPage=1;
	var toPage=1;
	
	// 首页
	if(id==1){
		currentPage = $("#currentPage1").text();
		if(currentPage>1){
			currentPage=1;
			fromPage=0;
			toPage=pageSize;
		}else{
			return false;
		}
	}
	
	// 上一页
	if(id==2){
		currentPage = $("#currentPage1").text();
		if(currentPage>1){
			fromPage = currentPage*pageSize-pageSize-pageSize;
			toPage = pageSize;
			currentPage--;
		}else{
			return false;
		}
	}
	
	// 下一页
	if(id==4){
		var total_record = $("#total_link").val();
		currentPage = $("#currentPage1").text();
		if(currentPage*pageSize<total_record){
			fromPage = (currentPage*pageSize);
			toPage = pageSize;
			currentPage++;
		}else{
			return false;
		}
	}
	
	// 尾页
	if(id==5){
		var total_record = $("#total_link").val();
		if(total_record>25 || total_record==25){
			if(total_record%pageSize==0){
				var pageCount = total_record/pageSize;
				fromPage = (pageCount-1)*pageSize;
				toPage = pageSize;
				currentPage = total_record/pageSize;
			}else{
				yushu = total_record%pageSize;
				pageCount = parseInt(total_record/pageSize);
				fromPage = pageCount*pageSize;
				toPage = yushu;
				currentPage = parseInt(total_record/pageSize)+1;
			}
		}else{
			return false;
		}
	}
	
	//var moduleId = $("#page_module_id").val();
	
	$.ajax({
		cache : true,
		type : "GET",
		url : "/get_link_list_page?pageSize=25&fromPage="+fromPage+"&toPage="+toPage,
		async : true,
		success : function(data) {
			$("#tb_link_list").empty();
			var th = "<tr><th style='width:10%'>链路ID</th><th  style='width:20%'>链路名称</th><th  style='width:30%'>链路节点序列</th><th  style='width:30%'>链路详情描述</th><th style='width:10%'>操作</th></tr>";
		    $("#tb_link_list").append(th);
		    if(data.length==0){
		    	var ps = "<tr><td colspan='8' style='text-align:center;font-weight:bold;color:red;'>what? 竟然一条测试链路都没有，快去添加！</td></tr>";
		    	$("#tb_link_list").append(ps);
		    }else{
		    	for (var i=0;i<data.length;i++){
		    		var content = "<tr><td>"+data[i].linkId+"</td><td><a href='javascript:void(0)' onclick='find_link_by_id("+data[i].linkId+")'>"+data[i].linkName+"</a></td><td>"+data[i].linkNodeSequence+"</td><td>"+data[i].linkDetail+"</td><td style='text-align:center;text-indent:-5px;'><a href='javascript:void(0)' onclick='link_execute_by_id("+data[i].linkId+")'>执行</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='delete_link_by_id("+data[i].linkId+")'>删除</a></td></tr>";
		    		$("#tb_link_list").append(content);
		    	}
		    }
		    $("#currentPage1").text(currentPage);
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function add_server_monitor() {
	
	// 服务名称校验
	var vserver_name = $("#server_name").val();
	if(vserver_name==""){
		if($("#vserver_name_tips").length == 0){
			$("#server_name").after("<span id='vserver_name_tips' style='color:red;'>服务名称不能为空！<span>");
			return false;
		}else if($("#vserver_name_tips").length > 1){
			$("#vserver_name_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vserver_name_tips").remove();
	}

	// 服务IP校验
	var vserver_ip = $("#server_ip").val();
	if(vserver_ip==""){
		if($("#vserver_ip_tips").length == 0){
			$("#server_ip").after("<span id='vserver_ip_tips' style='color:red;'>服务IP或域名不能为空！<span>");
			return false;
		}else if($("#vserver_ip_tips").length > 1){
			$("#vserver_ip_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vserver_ip_tips").remove();
	}
	
	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_server_monitor",
		data : $('#add-server-monitor-form').serialize(),
		async : true,
		success : function(data) {
			if (data == 1) {
				alert("添加监控成功！");
				window.location.reload();
			} else {
				alert("添加监控失败，请确认后重试！");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

$("#add_parameter").click(function () {
	var tab = document.getElementById("tb-global-parameter") ;
    var rows = tab.rows.length;
	var tb_tr = "<tr class='td'><td class='td'><input type='text' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数名'/></td><td class='td'><input type='text' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数值'/></td><td style='text-align: center;'><a href='javascript:void(0)' onclick='delete_parameter(this)'>删除</td></tr>";
	$("#tb-global-parameter").append(tb_tr);
});

function delete_parameter(obj){
	 var index = $(obj).parents("tr").index();
	 $(obj).parents("tr").remove();
}

$("#submit_parameter").click(function () {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_global_parameter",
		data : $('#global-parameter-form').serialize(),
		async : true,
		success : function(data) {
			if (data == 1) {
				alert("添加全局变量成功！");
				//window.location.reload();
			} else {
				alert("添加全局变量失败，请确认后重试！");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
})

function add_link_node(){
	var face_name = $('#link_face_list option:selected').text();
	var face_id = $('#link_face_list option:selected').val();
	var link_div = "<div class='link_node'>"+face_name+"</div>";
	var link_nodes = "<input type='hidden' name='link_nodes' id='link_nodes' value='"+face_id+"'>";
	var link_names = "<input type='hidden' name='link_names' id='link_names' value='"+face_name+"'>";
	$("#link_picture").append(link_div);
	$("#link_picture").append(link_nodes);
	$("#link_picture").append(link_names);
}

function add_link(){
	
	// 校验链路名称
	var vlink_name = $("#link_name").val();
	if(vlink_name==""){
		if($("#vlink_name_tips").length == 0){
			$("#link_name").after("<span id='vlink_name_tips' style='color:red;'>链路名称不能为空！<span>");
			return false;
		}else if($("#vlink_name_tips").length > 1){
			$("#vlink_name_tips").remove();
			return false;
		}
		return false;
	}else{
		$("#vlink_name_tips").remove();
	}

	if(confirm("提交以后各链路节点和节点顺序都将不可更改！\r\n确认提交 ？")==true){
		$("#hidden_link_id").empty();
		var link_id = $("#hidden_link_id").val();
		$.ajax({
			cache : true,
			type : "GET",
			url : "/add_link?linkId="+link_id,
			data : $('#add-link-test-form').serialize(),
			async : true,
			success : function(data) {
				if (data == 1) {
					$("#link_list").click();
				} else {
					alert("添加链路失败，请确认后重试！");
				}
			},
			error : function(request) {
				alert("Connection error:" + request.error);
			}
		});
	}else{
		return false;
	}
}

function find_link_by_id(id){
	$.ajax({
		cache : true,
		type : "GET",
		url : "/find_link_by_id?link_id="+id,
		async : true,
		success : function(data) {
			
			$("#link_picture").empty();
			
			$("#link_test").click();
			$("#link_name").val(data.linkName);
			$("#link_detail").text(data.linkDetail);
			var linkNodes = data.linkNodeSequence;
			var linkNames = data.linkNodeNameSequence;
			var linkNodeArrs = linkNodes.split("➜");
			var linkNodeNameArrs = linkNames.split("➜");
			for(var i=0;i<linkNodeArrs.length;i++){
				var link_div = "<div class='link_node'>"+linkNodeNameArrs[i]+"</div>";
				var link_node = "<input type='hidden' name='link_nodes' id='link_nodes' value='"+linkNodeArrs[i]+"'>";
				var link_name = "<input type='hidden' name='link_names' id='link_names' value='"+linkNodeNameArrs[i]+"'>";
				$("#link_picture").append(link_div);
				$("#link_picture").append(link_node);
				$("#link_picture").append(link_name);
			}
			
			var hidden_link_id = "<input id='hidden_link_id' type='hidden' value='"+id+"'>";
			$("#link_picture").append(hidden_link_id);
			
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
}

function link_execute_by_id(link_id){
	$.ajax({
		cache : true,
		type : "GET",
		url : "/link_execute_by_id?link_id="+link_id,
		async : true,
		success : function(data) {
			showbody(data);
			//alert("SUCCESS!\r\n"+data);
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});

}

function delete_link_by_id(link_id){
	if(confirm("确认要删除LinkID为"+link_id+"的链路吗？\r\n删除以后本条链路将不可恢复！")==true){
		$.ajax({
			cache : true,
			type : "GET",
			url : "/delete_link_by_id?link_id="+link_id,
			async : true,
			success : function(data) {
				$("#link_list").click();
			},
			error : function(request) {
				alert("Connection error:" + request.error);
			}
		});
	}else{
		return false;
	}
}

$("#add_env_config").click(function(){
	$("#env-config").fadeIn(135);
	// 查询全局参数并在页面显示
    $.ajax({
		cache : true,
		type : "POST",
		url : "/get_env_config",
		async : true,
		success : function(data) {
			if(data!=""){
				
				$("#tb-env-config").empty();
				
				var tb_th = "<tr><th class='th' style='width:45%'>参数名</th><th style='width:45%' class='th'>参数值</th><th style='width:10%' class='th'>操作</th></tr>";
				$("#tb-env-config").append(tb_th);
				for(var key in data){
					var tab = document.getElementById("tb-env-config");
					var rows = tab.rows.length;
					var tb_tr = "<tr class='td'><td class='td'><input type='text' value='"+key+"' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数名'/></td><td class='td'><input type='text' value='"+data[key]+"' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数值'/></td><td style='text-align: center;'><a href='javascript:void(0)' onclick='delete_parameter(this)'>删除</td></tr>";
					$("#tb-env-config").append(tb_tr);
				}
			}else{
				return false;
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
})

$("#env-config-title-closed").click(function(){
	$("#env-config").fadeOut(135);
})

$("#add-config-item").click(function () {
	var tab = document.getElementById("tb-env-config") ;
    var rows = tab.rows.length;
	var tb_tr = "<tr class='td'><td class='td'><input type='text' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数名'/></td><td class='td'><input type='text' class='input-text-param' id='param_name_"+rows+"' name='param_name_"+rows+"' placeholder='参数值'/></td><td style='text-align: center;'><a href='javascript:void(0)' onclick='delete_parameter(this)'>删除</td></tr>";
	$("#tb-env-config").append(tb_tr);
});

$("#submit_config").click(function () {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/add_env_config",
		data : $('#env-config-form').serialize(),
		async : true,
		success : function(data) {
			if (data == 1) {
				alert("添加成功！");
			} else {
				alert("failed");
			}
		},
		error : function(request) {
			alert("Connection error:" + request.error);
		}
	});
})

function showbody(responsebody){
	$("#response-body-content").text(responsebody);
	$("#response-body").fadeIn(125);
};

$("#response-body-title-closed").click(function(){
	$("#response-body").fadeOut(100);
});

$("#about_sys").click(function(){
	$("#about").fadeIn(125);
});

$("#about-title-closed").click(function(){
	$("#about").fadeOut(100);
});
